package cn.b.training.baby_education_training.service.impl;

import cn.b.training.baby_education_training.config.BaiduAiProperties;
import cn.b.training.baby_education_training.entity.FileInfo;
import cn.b.training.baby_education_training.service.FileService;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bos.BosClient;
import com.baidubce.services.bos.BosClientConfiguration;
import com.baidubce.services.bos.model.PutObjectResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * 使用百度BOS的文件存储方案
 */
@Service("FileServiceBos")
public class FileServiceBosImpl implements FileService {
    @Autowired
    BaiduAiProperties baiduAiProperties;

    @Override
    public FileInfo uploadFile(MultipartFile file) throws IOException {
        //1、从MultipartFile中获得原始文件名 originalName。
        String originalFileName=file.getOriginalFilename();
        //2、从MultipartFile中获得文件大小 fileSize。
        Long fileSize = file.getSize();
        //3、解析原始文件名，得到后缀名 extName。
        String extName="";
        if(originalFileName.indexOf(".")>0){
            extName= originalFileName.substring(originalFileName.lastIndexOf("."));
        }
        //4、通过UUID生成新的文件名，拼接后缀名得到完整的新的服务端文件名 serverFileName。
        String serverFileName= UUID.randomUUID().toString()+extName;
        //5、调用BOS的sdk，将文件写入bos。
        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(baiduAiProperties.getAccessKeyId(), baiduAiProperties.getSecretAccessKey()));
        config.setEndpoint(baiduAiProperties.getEndpoint());
        BosClient client = new BosClient(config);
        PutObjectResponse putObjectFromFileResponse = client.putObject("baby", serverFileName, file.getInputStream());
        //6、组织文件在bos中的访问地址 path。
        String path="http://"+baiduAiProperties.getEndpoint()+"/baby"+"/"+serverFileName;
        //7、新建FileInfo对象：FileInfo fileInfo=new FileInfo()，完成5个字段赋值，返回fileInfo.
        FileInfo fileInfo=new FileInfo();
        fileInfo.setOriginalName(originalFileName);
        fileInfo.setExtName(extName);
        fileInfo.setServerFileName(serverFileName);
        fileInfo.setPath(path);
        fileInfo.setFileSize(fileSize);
        return fileInfo;
    }

    @Override
    public FileInfo uploadFile2(MultipartFile file) throws IOException {
        //1、从MultipartFile中获得原始文件名 originalName。
        String originalFileName=file.getOriginalFilename();
        //2、从MultipartFile中获得文件大小 fileSize。
        Long fileSize = file.getSize();
        //3、解析原始文件名，得到后缀名 extName。
        String extName="";
        if(originalFileName.indexOf(".")>0){
            extName= originalFileName.substring(originalFileName.lastIndexOf("."));
        }
        //4、通过UUID生成新的文件名，拼接后缀名得到完整的新的服务端文件名 serverFileName。
        String serverFileName= UUID.randomUUID().toString()+extName;
        //5、调用BOS的sdk，将文件写入bos。
        BosClientConfiguration config = new BosClientConfiguration();
        config.setCredentials(new DefaultBceCredentials(baiduAiProperties.getAccessKeyId(), baiduAiProperties.getSecretAccessKey()));
        config.setEndpoint(baiduAiProperties.getEndpoint());
        BosClient client = new BosClient(config);
        PutObjectResponse putObjectFromFileResponse = client.putObject("note", serverFileName, file.getInputStream());
        //6、组织文件在bos中的访问地址 path。
        String path="http://"+baiduAiProperties.getEndpoint()+"/note"+"/"+serverFileName;
        //7、新建FileInfo对象：FileInfo fileInfo=new FileInfo()，完成5个字段赋值，返回fileInfo.
        FileInfo fileInfo=new FileInfo();
        fileInfo.setOriginalName(originalFileName);
        fileInfo.setExtName(extName);
        fileInfo.setServerFileName(serverFileName);
        fileInfo.setPath(path);
        fileInfo.setFileSize(fileSize);
        return fileInfo;
    }
}
